package ctm.javacode;
import java.awt.AlphaComposite;
import java.awt.Color;
import java.awt.Composite;
import java.awt.Font;
import java.awt.Graphics2D;
import java.awt.font.FontRenderContext;
import java.awt.font.LineMetrics;
import java.awt.font.TextAttribute;
import java.awt.geom.AffineTransform;
import java.awt.geom.Arc2D;
import java.awt.geom.Line2D;
import java.awt.geom.Point2D;
import java.awt.geom.Rectangle2D;
import java.text.NumberFormat;
import java.util.HashMap;

import org.jfree.chart.plot.SpiderWebPlot;
import org.jfree.data.category.*;



public class mySpiderWebPlot extends SpiderWebPlot{
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private int ticks = DEFAULT_TICKS; 
    private static final int DEFAULT_TICKS = 10; 
    private NumberFormat format = NumberFormat.getInstance(); 
    private static final double PERPENDICULAR = 90; 
    private static final double TICK_SCALE = 0.015; 
    private int valueLabelGap = DEFAULT_GAP; 
    private static final int DEFAULT_GAP = 10; 
    private static final double THRESHOLD = 15; 
    private static final double LABELANDAXISGAP = 10;  
    private static final double LABELANDCENTERGAP = 1;
    public mySpiderWebPlot(CategoryDataset createCategoryDataset) { 
        super(createCategoryDataset); 
    } 
@Override 
	protected void drawLabel(final Graphics2D g2, final Rectangle2D plotArea, final double value, 
                final int cat, final double startAngle, final double extent) { 
    super.drawLabel(g2, plotArea, value, cat, startAngle, extent); 
    final FontRenderContext frc = g2.getFontRenderContext(); 
    final double[] transformed = new double[2]; 
    final double[] transformer = new double[2]; 
    final Arc2D arc1 = new Arc2D.Double(plotArea, startAngle, 0, Arc2D.OPEN);
    for (int i = 1; i <= ticks; i++) { 
    	if(i == 3 || i == 4 || i == 6){
            final Point2D point1 = arc1.getEndPoint(); 
            final double deltaX = plotArea.getCenterX(); 
            final double deltaY = plotArea.getCenterY(); 
            double labelX = point1.getX() - deltaX; //System.out.println(labelX);
            double labelY = point1.getY() - deltaY; //System.out.println(labelY);
            final double scale = ((double) i / (double) ticks); 
            final AffineTransform tx = AffineTransform.getScaleInstance(scale, scale); 
            final AffineTransform pointTrans = AffineTransform.getScaleInstance(scale + TICK_SCALE, scale + TICK_SCALE); 
            transformer[0] = labelX; 
            transformer[1] = labelY; 
            pointTrans.transform(transformer, 0, transformed, 0, 1); 
            final double pointX = transformed[0] + deltaX; //System.out.println(pointX);
            final double pointY = transformed[1] + deltaY; //System.out.println(pointY);
            tx.transform(transformer, 0, transformed, 0, 1); 
            labelX = transformed[0] + deltaX; //System.out.println(labelX);
            labelY = transformed[1] + deltaY; //System.out.println(labelY);
            double rotated = (PERPENDICULAR); 
            AffineTransform rotateTrans = AffineTransform.getRotateInstance(Math.toRadians(rotated), labelX, labelY); 
            transformer[0] = pointX; 
            transformer[1] = pointY; 
            rotateTrans.transform(transformer, 0, transformed, 0, 1); 
            final double x1 = transformed[0]; //System.out.println(x1);
            final double y1 = transformed[1]; //System.out.println(y1);
            rotated = (-PERPENDICULAR); 
            rotateTrans = AffineTransform.getRotateInstance(Math.toRadians(rotated), labelX, labelY); 
            rotateTrans.transform(transformer, 0, transformed, 0, 1); 
            final Composite saveComposite = g2.getComposite(); 
            g2.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 1.0f)); 
            g2.draw(new Line2D.Double(transformed[0], transformed[1], x1, y1)); 
            if (startAngle == this.getStartAngle()) { 
            	
/*有改 */                     final String label = format.format(((double) i / (double) ticks) * getMaxValue()); //System.out.println(label);
                    //final Rectangle2D labelBounds = getLabelFont().getStringBounds(label, frc); 
                    final LineMetrics lm = getLabelFont().getLineMetrics(label, frc); 
                    final double ascent = lm.getAscent();  //System.out.println(ascent);
                    //System.out.println(labelX);
                    //System.out.println(plotArea.getCenterX()+"end");
                    if (Math.abs(labelX - plotArea.getCenterX()) < THRESHOLD) { 
                            labelX += valueLabelGap; //System.out.println(labelX);
                            labelY += ascent / (float) 2; //System.out.println(labelY);
                    } else if (Math.abs(labelY - plotArea.getCenterY()) < THRESHOLD) { 
                            labelY += valueLabelGap; 
                    } else if (labelX >= plotArea.getCenterX()) { 
                            if (labelY < plotArea.getCenterY()) { 
                                    labelX += valueLabelGap; 
                                    labelY += valueLabelGap; 
                            } else { 
                                    labelX -= valueLabelGap; 
                                    labelY += valueLabelGap; 
                            } 
                    } else { 
                            if (labelY > plotArea.getCenterY()) { 
                                    labelX -= valueLabelGap; 
                                    labelY -= valueLabelGap; 
                            } else { 
                                    labelX += valueLabelGap; 
                                    labelY -= valueLabelGap; 
                            } 
                    }
                    g2.setPaint(getLabelPaint()); 
                    g2.setFont(new Font("標楷體", Font.BOLD, 18)); 
                    g2.drawString(label, (float) labelX, (float) labelY); 
           } 
            g2.setComposite(saveComposite); 
    	}
    	else;
    } 
    
    
    
    
    
    
    
    
    
    
    /**
	 * @param sumAxisNum : the sum number of the axis.
	 * @param centerX : the center X coordinate of the panel.
	 * @param centerY : the center Y coordinate of the panel.
	 */
	int sumAxisNum = this.getDataset().getColumnCount();
	double centerX = plotArea.getCenterX();
	double centerY = plotArea.getCenterY();
	Arc2D arc2D;
	FontRenderContext fontRenderContext = g2.getFontRenderContext();

	for (int currentAxisNum = 0; currentAxisNum < sumAxisNum; currentAxisNum++) {

		/**
		 * @param currentValue : the value of the current axis.
		 * @param label : the value on the label.
		 * @param scale : the scale of the point value to the max value on
		 *            current axis.
		 */
		double currentValue = this.getDataset().getValue(0, currentAxisNum).doubleValue();
		String label = String.valueOf((int) currentValue);
		double scale = (currentValue / this.getMaxValue());
		/**
		 * the coordinate of the label
		 */

		if (startAngle == this.getStartAngle()) {
			double labelX;
			double labelY;
			/**
			 * @param currentAngle : the angle of the current axis where the label place.
			 */
			double currentAngle = -(extent * currentAxisNum) + startAngle;
			if(currentAngle < 0) currentAngle = 360 + currentAngle;
			arc2D = new Arc2D.Double(plotArea, currentAngle, 0, Arc2D.OPEN);
			Point2D point2D = arc2D.getEndPoint();
			double axisX = Math.abs(point2D.getX() - centerX)* scale;
			double axisY = Math.abs(point2D.getY() - centerY)* scale;

			if (currentAngle >= 0 && currentAngle < 90) {
				labelX = centerX + axisX;
				labelY = centerY - axisY;
			} else if (currentAngle < 180) {
				labelX = centerX - axisX;
				labelY = centerY - axisY;
			} else if (currentAngle < 270) {
				labelX = centerX - axisX;
				labelY = centerY + axisY;
			} else {
				labelX = centerX + axisX;
				labelY = centerY + axisY;
			}
			
			LineMetrics lm = getLabelFont().getLineMetrics(label,
					fontRenderContext);
			double ascent = lm.getAscent();

			/**
			 * set the gap between the label and axis, or between the label
			 * and the center point.
			 */
			if (Math.abs(labelX - plotArea.getCenterX()) < LABELANDCENTERGAP) {
				labelX += LABELANDAXISGAP;
				labelY += ascent / (float) 2;
			} else if (Math.abs(labelY - plotArea.getCenterY()) < LABELANDCENTERGAP) {
				labelY += LABELANDAXISGAP;
			} else if (labelX >= plotArea.getCenterX()) {
				if (labelY < plotArea.getCenterY()) {
					labelX += LABELANDAXISGAP;
					labelY += LABELANDAXISGAP;
				} else {
					labelX -= LABELANDAXISGAP;
					labelY += LABELANDAXISGAP;
				}
			} else {
				if (labelY > plotArea.getCenterY()) {
					labelX -= LABELANDAXISGAP;
					labelY -= LABELANDAXISGAP;
				} else {
					labelX += LABELANDAXISGAP;
					labelY -= LABELANDAXISGAP;
				}
			}
			//set label font
			HashMap<TextAttribute, Comparable<?>> hm = new HashMap<TextAttribute, Comparable<?>>();
			hm.put(TextAttribute.SIZE,14); 
			hm.put(TextAttribute.FAMILY,"Impact");//
			Font font = new Font(hm);

			g2.setPaint(Color.blue);
			g2.setFont(font);
			//g2.drawString(label, (float) labelX, (float) labelY);
		}
	}

	}
}
